package PhotoController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.PagingManager;


public class PhotoOldAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String sessionID  = null;
	private HttpSession session = null;
	PhotoOldDAO photoOldDAO=new PhotoOldDAO();
    public PhotoOldAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	private int currentPage=1;
	
 	private PagingManager paging;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PhotoOldBean photoOldBean= null;
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		RequestDispatcher view = null;
		if(action.equals("pt_old_list_main")){
			System.out.println("중고년사진");
			ArrayList list =photoOldDAO.pt_old_list_main();
			request.setAttribute("list", list);
			request.getRequestDispatcher("/Main/OldMain/home_photo2.jsp").forward(request, response);
		}
		else if(action.equals("pt_old_list")){
			String mem_nick=request.getParameter("nick");
			int level=photoOldDAO.young_level(mem_nick);	
			
			if(request.getParameter("currentPage")!=null){
				currentPage=Integer.parseInt(request.getParameter("currentPage"));
			}
			HashMap param=new HashMap();
			param = new HashMap<String, Object>();
			param.clear();
			param.put("pageSize", 5 );
			param.put("currentPage", currentPage );
			param.put("pageGroupSize", 5 );
			int pageNo = ((Integer)param.get("currentPage") -1) * (Integer)param.get("pageSize");
			int endNum = pageNo + (Integer)param.get("pageSize");
			param.put( "startNum", pageNo );
			//param.put( "recordCount", (Integer)param.get("pageSize") );
			ArrayList list =photoOldDAO.pt_old_list(param);
			int totalCount = photoOldDAO.pfTotalCount(param);
			param.put("totalCount", totalCount);
			paging=new common.PagingManager();
			paging.setPageNo(param);
			
			
			session = request.getSession();
			session.setAttribute("list", list);
			session.setAttribute("paging", paging.getReturnHtml());
			request.setAttribute("page","/photo/PtOld/PT_Old_List");
			if(level==2){
				session.setAttribute("photo", "old_photo");
				view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
				
				}else if(level==1){
				session.setAttribute("photo", "old_photo2");
				view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
				}
			view.forward(request, response);
			//request.setAttribute("list", list);
			//request.setAttribute("paging", paging.getReturnHtml());
			//request.getRequestDispatcher("/photo/PtOld/PT_Old_List.jsp").forward(request, response);
	}else if(action.equals("pt_old_recom")){
			String mem_nick=request.getParameter("nick");
			int id=Integer.parseInt(request.getParameter("id"));
			ArrayList list=photoOldDAO.pt_old_recom(id);
			int level=photoOldDAO.young_level(mem_nick);
			if(level==2){
				session.setAttribute("photo", "old_photo");
			
			}else if(level==1){
				session.setAttribute("photo", "old_photo2");
			}
			request.setAttribute("list", list);
			request.setAttribute("id", id);
			request.getRequestDispatcher("/photo/PtOld/PT_Old_Comment.jsp").forward(request, response);
	}else if(action.equals("pt_old_recom_write")){
		System.out.println("aaa");
		int id=Integer.parseInt(request.getParameter("id"));
		String oc_nick=request.getParameter("oc_nick");
		String oc_contents=request.getParameter("oc_contents");
		System.out.println(oc_contents);
		
		ArrayList list=photoOldDAO.pt_old_recom_write(id,oc_nick,oc_contents);
		request.setAttribute("list", list);
		request.setAttribute("id", id);
		request.getRequestDispatcher("/PhotoOldAction.do?action=pt_old_recom&id="+id).forward(request, response);
	}else if(action.equals("pt_old_com_cancle")){
		int com_id=Integer.parseInt(request.getParameter("id"));
		int oldplus_id=Integer.parseInt(request.getParameter("oldplus_id"));
		photoOldDAO.pt_old_com_cancle(com_id,oldplus_id);
		request.getRequestDispatcher("/PhotoOldAction.do?action=pt_old_recom&id="+oldplus_id).forward(request, response);
	}
	}

}
